package leetcode;

import java.util.PriorityQueue;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q1046.java
 * @Author : WangRuoyu
 * @Date 2023/11/5 22:30
 */


public class Q1046 {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> o2 - o1);
        for (int i = 0; i < stones.length; i++) {
            pq.add(stones[i]);
        }
        while (pq.size() > 1) {
            int y = pq.poll();
            int x = pq.poll();
            pq.add(y - x);
        }
        return pq.peek();
    }

    public static void main(String[] args) {
        Q1046 sl = new Q1046();
        int[] stones = {2, 7, 4, 1, 8, 1};
        System.out.println(sl.lastStoneWeight(stones));
    }
}